<template>
  <el-card class="box-card">
    <div class="model-role">
      <el-form :inline="true" :model="dataForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="dataForm.equipmentName" clearable placeholder="商品名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.customerName" clearable placeholder="客户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDateList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openDialog()">添加订单</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border style="width: 100%">
        <el-table-column prop="id" label="订单编号" width="250"></el-table-column>
        <el-table-column prop="goodsName" label="商品" ></el-table-column>
        <el-table-column prop="customerName" label="客户" ></el-table-column>
        <el-table-column prop="goodsNumber" label="商品的数量" ></el-table-column>
        <el-table-column prop="areaName" label="区域" ></el-table-column>
        <el-table-column prop="createTime" label="下单时间" ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        style="margin-top: 30px">
      </el-pagination>
    </div>

    <el-dialog :title="dataDialogForm.id === '' ?  dialogTitle:dialogTitle='更新订单信息' "
               :visible.sync="dialogFormVisible" @close="closeDialog()">
      <el-form :model="dataDialogForm" :rules="rules" ref="ruleForm">

        <el-form-item label="客户" :label-width="formLabelWidth">
          <el-select v-model="dataDialogForm.customerId" placeholder="请选择客户" style="width: 100%">
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.customerName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备" :label-width="formLabelWidth">
          <el-select v-model="dataDialogForm.goodsId" placeholder="请选择设备" style="width: 100%">
            <el-option v-for="item in materialList" :key="item.id" :label="item.equipmentName"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备数量" :label-width="formLabelWidth">
          <el-input v-model="dataDialogForm.goodsNumber" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="区域" :label-width="formLabelWidth">
          <el-select v-model="dataDialogForm.area" placeholder="请选择区域" style="width: 100%">
            <el-option v-for="item in regionList" :key="item.id" :label="item.area"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="dataDialogForm.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm()">确 定</el-button>
      </div>
    </el-dialog>

  </el-card>
</template>

<script>

export default {
  name: 'order',
  data() {
    
    return {
      dialogTitle: '添加订单',
      dataForm: {
        equipmentName: "",
        customerName: "",
      },
      ruleForm: {
        pass: '',
      },

      dataDialogForm: {
        id: '',
        customerId: '',
        goodsId: '',
        goodsNumber: '',
        area:'',
        address: '',
        createTime: '',
      },
      materialList:[],
      customerList:[],
      regionList:[],
      businessList:[],
      dataList: [],
      userName: "xiaohei",
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      datalistLoading: false,
      dialogFormVisible: false,
      dialogFormSubmitVisible: false,
      formLabelWidth: '100px',
      rules: {

      }
    };
  },
  methods: {
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDateList()
    },
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDateList()
    },
    getDateList() {
      // this.datalistLoading = true
      //生命params 分页查询的参数
      const params = {
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          equipmentName: this.dataForm.equipmentName,
          customerName: this.dataForm.customerName,
          
        }
      }
      this.$http.get('/orders/findByPage', params).then((res) => {
        console.log(res)
        this.dataList = res.data.data.records
        this.totalPage = res.data.data.total
      })
    },
    handleEdit(index, item) {
      //打开更新窗口

      this.dataDialogForm.id = item.id
      this.dataDialogForm.customerId = item.customerId
      this.dataDialogForm.goodsId = item.goodsId
      this.dataDialogForm.goodsNumber = item.goodsNumber
      this.dataDialogForm.area = item.area
      this.dataDialogForm.address = item.address
      this.dataDialogForm.createTime = item.createTime

      this.openDialog();
    },
    handleDelete(index, item) {
      this.$confirm('此操作将永久删除该区域信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/orders/' + item.id).then((res) => {
          this.getDateList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加用户按钮绑定的方法
    openDialog() {

      //打开窗口
      this.dialogFormVisible = true
    },
    closeDialog() {
      //关闭窗口
      this.dialogFormVisible = false;

      //所有字段置空
      this.dataDialogForm = {
        id: '',
        customerId: '',
        goodsId: '',
        goodsNumber: '',
        area:'',
        address: '',
        createTime: '',
      }
    },
    handleConfirm() {
      // 当点击确定按钮时，访问当前对话框的标题
      const title = this.dialogTitle;
      console.log('当前对话框标题:', title);

      if (title === '添加订单') {
        this.handleSubmitFormData('ruleForm');
      } else {
        this.handleUpdateFormData('ruleForm');
      }
    },
    handleSubmitFormData(ruleForm) {
      //打开窗口
      // this.dialogFormVisible = true
      this.addOrders(ruleForm)
    },
    handleUpdateFormData(ruleForm) {
      this.updateOrders(ruleForm)
    },
    updateOrders(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        this.$http.put('/orders', this.dataDialogForm).then((res) => {
          console.log(res)
          this.dialogFormVisible = false   //关闭窗口
          this.closeDialog();
          this.getDateList();
        })
      });
    },
    addOrders(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
            this.$http.post('/orders', this.dataDialogForm).then((res) => {
              console.log(res)  
              this.closeDialog()  //关闭窗口
              this.getDateList();
              if(res.data.code!==200){
                this.$message({
                  type: 'error',
                  message: '添加失败'
                });
              }
            })
            return
        } else {
          return false;
        }
      });
      
    },
    getMaterialList() {
      const params = {params:{pageIndex:1,pageSize:1000}}
      this.$http.get('/material/findByPage',params
      ).then((res)=>{
        console.log(res)
        this.materialList = res.data.data.records
      })
    },
    getCustomerList() {
      const params = {params:{pageIndex:1,pageSize:1000}}
      this.$http.get('/customer/findByPage',params
      ).then((res)=>{
        console.log(res)
        this.customerList = res.data.data.records
      })
    },
    getRegionList() {
      const params = {params:{pageIndex:1,pageSize:1000}}
      this.$http.get('/region/findByPage',params
      ).then((res)=>{
        console.log(res)
        this.regionList = res.data.data.records
      })
    },
  },

  mounted() {
    this.getDateList()
    this.getMaterialList();
    this.getCustomerList();
    this.getRegionList();
  },
}

</script>

